package com.ponhu.ea2.stockcenter.controller;

import com.ponhu.ea2.common.basic.model.PagedResult;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.stockcenter.dto.StockBatchChangeQueryDTO;
import com.ponhu.ea2.stockcenter.dto.StockBatchQueryDTO;
import com.ponhu.ea2.stockcenter.enums.ExchangeActionTypeEnum;
import com.ponhu.ea2.stockcenter.service.bms.StockBatchService;
import com.ponhu.ea2.stockcenter.vo.ActionTypeVO;
import com.ponhu.ea2.stockcenter.vo.ReturnResponseBean;
import com.ponhu.ea2.stockcenter.vo.StockBatchChangeVO;
import com.ponhu.ea2.stockcenter.vo.StockBatchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/stockBatch")
@Api(tags = "批次库存接口", value = "StockBatchController")
public class StockBatchController {

    @DubboReference(version = "1.0.0", check = false)
    private StockBatchService stockBatchService;

    /**
     * 条件查询货品批次库存变动记录列表
     */
    @ApiOperation(value = "条件查询货品批次库存变动记录列表")
    @PostMapping("/getStockBatchChangeListByQuery")
    public ResponseBean<PagedResult<StockBatchChangeVO>> getStockBatchChangeListByQuery(
            @RequestBody StockBatchChangeQueryDTO queryDTO) {

        return ResponseBean.ok(stockBatchService.getStockBatchChangeListByQuery(queryDTO));
    }

    /**
     * 根据货品编码查询批次库存列表
     */
    @ApiOperation(value = "根据货品编码查询批次库存列表")
    @PostMapping("/getStockBatchListByInventoryCode")
    public ReturnResponseBean<PagedResult<StockBatchVO>> getStockBatchListByInventoryCode(
            @RequestBody @Validated StockBatchQueryDTO queryDTO) {

        return ReturnResponseBean.ok(stockBatchService.getStockBatchListByInventoryCode(queryDTO));
    }

    /**
     * 获取全部批次库存变动类型
     */
    @ApiOperation(value = "获取全部批次库存变动类型")
    @GetMapping("/getAllStockBatchActionTypes")
    public ReturnResponseBean<List<ActionTypeVO>> getAllStockBatchActionTypes() {

        // 出库相关
        List<ExchangeActionTypeEnum> outEnumList = Arrays.asList(
                ExchangeActionTypeEnum.PURCHASE_RETURN_OUT,
                ExchangeActionTypeEnum.CHECK_LOSS_OUT,
                ExchangeActionTypeEnum.SALES_OUT);

        // 入库相关
        List<ExchangeActionTypeEnum> inEnumList = Arrays.asList(
                ExchangeActionTypeEnum.SALES_RETURN_IN,
                ExchangeActionTypeEnum.CHECK_PROFIT_IN,
                ExchangeActionTypeEnum.PURCHASE_IN);

        List<ExchangeActionTypeEnum> enumList = Lists.newArrayList();
        enumList.addAll(outEnumList);
        enumList.addAll(inEnumList);

        return ReturnResponseBean.ok(enumList.stream().map(em ->
                        new ActionTypeVO()
                                .setCode(em.getCode())
                                .setName(em.getName())
                                .setToString(em.toString()))
                .collect(Collectors.toList()));
    }

}
